<?php defined( '_VALID_MOS' ) or die( 'Direct Access to this location is not allowed.' );

/* JoomLeague League Management and Prediction Game for Joomla!
 * @version $Id: showresultsranking.php 1203 2008-07-10 08:20:27Z julienv $
 * @package Joomleague
 * @copyright Copyright (C) 2007  JoomLeague
 * @license http://www.gnu.org/copyleft/gpl.html GNU/GPL, see _joomleague_license.txt
 *  
 * Homepage: http://www.joomleague.de
 * Support: htt://www.joomleague.de/forum/
 * 
 * This file is part of JoomLeague.
 *  
 * JoomLeague is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * Please note that the GPL states that any headers in files and
 * Copyright notices as well as credits in headers, source files
 * and output (screens, prints, etc.) can not be removed.
 * You can extend them with your own credits, though...
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 *
 * The "GNU General Public License" (GPL) is available at
 * http://www.gnu.org/copyleft/gpl.html.
*/ 
  
  /**
   * Creates a form with current project rounds selector
   *
   * @param string $jlfunc requested 'func'
   * @param int $total_rounds
   * @param int $current_round
   * @return html           
   */
  function resultsRankingNav( $jl_func, $total_rounds, $current_round=0 )
  {
      global $joomleague;
      $divLevel = intval(mosGetParam($_REQUEST, 'divLevel', 0));
      $division = intval(mosGetParam($_REQUEST, 'division', 0));
      $layout   = mosGetParam($_REQUEST, 'layout', '');
      
      $pageNav2=<<<EOL
          <form action="" method="get" style="display:inline;">
          <select class="inputbox" onchange="joomleague_changedoc(this)">
EOL;
      
      $url = JL_COMPONENT_LINK.'&amp;func='.$jl_func.'&amp;p='.$joomleague->id;
      if ($division > 0) $url .= '&amp;division='.$division;
      if ($divLevel > 0) $url .= '&amp;divLevel='.$divLevel;
      if ($layout != '') $url .= '&amp;layout='.$layout;
      
      $url .= JL_ITEMID_SUFFIX;
      for ($counter = 1;$counter<=$total_rounds;$counter++)
      {
          if ($counter<10) $pagenumber="0".$counter;
          else $pagenumber=$counter;
          if ($counter<=$total_rounds)
          {
              $pageNav2.="<option value='";
              $pageNav2.=sefRelToAbs($url.'&amp;r='.$counter)."'";
              if ($counter==$current_round) { $pageNav2.=" selected='selected'"; }
              $pageNav2.=">";
          }
          $pageNav2.="$pagenumber&nbsp;&nbsp;</option>";
      }
      $pageNav2.=<<<EOL
          </select></form>
EOL;
      return $pageNav2;
  }
  
  function showResultsRanking ($jl_func, $r, &$overall_config) 
  {
    global $mainframe, $database, $my;
    global $joomleague;
    global $htmlHelper;
    
    $type = intval(mosGetParam($_REQUEST, 'type', 0));
    $layout = mosGetParam($_REQUEST, 'layout', '');
    
    if ( $layout == '' ) // all the results, then all the tables
    {
      showResults( $jl_func, $r, $overall_config );
      showRanking( $jl_func, $r, $overall_config );
    }
    else if ( $layout == 'bydiv' ) // blocks of results+ranking per division
    {
      $round = ($r == 0) ? $joomleague->current_round : $r;
      
      // get configuration from template
      $results_config=$joomleague->getTemplateConfig("show_results.tpl");
      $table_config=$joomleague->getTemplateConfig("show_table.tpl");
      
      $mainframe->setPageTitle(_JL_PAGE_TITLE_RANKING);
      
      // Find out if user has edit rights
      if( isset($my) && (defined('JL_USER_IS_PROJECT_ADMIN') 
                          || defined('JL_USER_IS_PROJECT_EDITOR' )
                          || defined('JL_USER_IS_BIG_BOSS') 
                          || $joomleague->isTeamEditor($round, $my->id)) ) {
        define('JL_SHOW_EDIT_ICON', TRUE);
      }
      
      //for sub division ranking tables
      if ( $joomleague->project_type=='DIVISIONS_LEAGUE' )
      {
      	$selDivision = intval(mosGetParam($_REQUEST, 'division', 0));
      	$divLevel    = intval(mosGetParam($_REQUEST, 'divLevel', $table_config['default_division_view']));
      	
      	if ($selDivision > 0) {
      	  $divisions = array($selDivision);
      	}
      	else 
        {
      		// check if division level view is allowed. if not, replace with default
  	      if (    ( $divLevel == 0 && $table_config['show_project_table']==0 )
  	           || ( $divLevel == 1 && $table_config['show_level1_table']== 0 )
  	           || ( $divLevel == 2 && $table_config['show_level2_table']==0 ) ) {
  	        $divLevel = $table_config['default_division_view'];
  	      }
  	      if ( $divLevel ) $divisions = $joomleague->getDivisionsId( $divLevel );
  	      else $divisions = array(0); //project
      	}
      }
      else $divisions = array(0); //project
      
      //create ranking object
      $ranking = new Ranking();
      $ranking->setProjectConfig($joomleague, $table_config['rankingorder']);
      $previous_ranking=array();
      
    	$from = 1;
    	$to   = $round;
                 
      foreach ( $divisions as $divid )
      {
        if ($divid > 0)
        {
          $division = new LeagueDivision($database);
      	  $division->load($divid);
      	}
        // get matches
        $matches[$divid] = $joomleague->getResultsRows( $round, $divid, $results_config );
        
        // get rankings
        $current_ranking[$divid] = $ranking->getRanking(0, $to, $from, $table_config['show_negative_points'], $table_config['sort_table'], $divid);
        if( $table_config['last_ranking']==1 )
        {
          if ( $to == 1 || ( $to == $from ) ) {//no previous ranking...
          	$previous_ranking[$divid] = &$current_ranking[$divid];
          }
        	else {
            $previous_ranking[$divid] = $ranking->getRanking(0, $to - 1, $from, $table_config['show_negative_points'], $table_config['sort_table'], $divid);
          }
        }
      }
      
      // get teams
      $resultTeams = $joomleague->getTeamsIndexedById();
      
      // get rounds name
      $rounds=$joomleague->getRoundNames();
      
      // colors in ranking
      $colors = $joomleague->getColors($table_config['colors']);
      
      // pages navigation
  	  $pageNav   = resultsRankingNav( $jl_func,count($rounds), $round );
  	  
      // display
	    if ( $layout == '' ) // all the results, then all the tables
	    {
	      require( getTemplateFile(JL_TEMPLATE_PATH, 'show_results_ranking.tpl.php') );
	    }
      else {
        require( getTemplateFile(JL_TEMPLATE_PATH, 'show_results_ranking_div.tpl.php') );
      }
    }
  }
?>
